<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
	</head>

	<body style="padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<form id="ff">
			<table id="table" class="displaytable" cellpadding="6" style="padding: 20px;">
				<tr>
					<td width="20%">
						<h id="star">*</h> 目标库房</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="STOREHOUSE_NAME" id="input_STOREHOUSE_NAME" class="easyui-combobox" data-options="required:true" />
					</td>
					<td width="20%">
						<h id="star">*</h> 数量</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="AMOUNT" id="input_AMOUNT" class="easyui-numberbox" data-options="required:true" />
					</td>
					<td width="20%">
						<h id="star">*</h> 商品名称</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="GOODS_NAME" id="input_GOODS_NAME" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
				</tr>
				<tr>
					<td>
						<h id="star">*</h> 入库时间</td>
					<td>
						<input type="text" style="width: 100%;" name="IN_DATE" id="input_IN_DATE" class="easyui-datebox" data-options="disabled:true,required:true" />
					</td>
					<td>
						<h id="star">*</h> 预存时间</td>
					<td>
						<input type="text" style="width: 100%;" name="EXT_DATE" id="input_EXT_DATE" class="easyui-datebox" data-options="disabled:true,required:true" />
					</td>
					<td>
						<h id="star">*</h> 单位</td>
					<td>
						<input type="text" style="width: 100%;" name="UNIT" id="input_UNIT" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
				</tr>
				<tr>
					<td>
						<h id="star">*</h> 规格</td>
					<td>
						<input type="text" style="width: 100%;" name="SPEC" id="input_SPEC" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
					<!--<td width="20%">
						<h id="star">*</h> 搬运费用</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="HAND_FEE" id="input_HAND_FEE" class="easyui-numberbox feeinput" data-options="precision:2"/>
					</td>-->
					<td>
						其他费用</td>
					<td>
						<input type="text" style="width: 100%;" name="OTHRE_FEE" id="input_OTHRE_FEE" class="easyui-numberbox feeinput" data-options="precision:2" />
					</td>
					<!--<td>
						<h id="star">*</h> 应收费用</td>
					<td>
						<input type="text" style="width: 100%;" name="SUM" id="input_SUM" class="easyui-numberbox" data-options="required:true,precision:2" />
					</td>-->
				</tr>
				<!--<tr>
					
					<td>
						<h id="star">*</h> 实收费用</td>
					<td>
						<input type="text" style="width: 100%;" name="REAL_SUM" id="input_REAL_SUM" class="easyui-numberbox" data-options="required:true,precision:2" />
					</td>
					
				</tr>-->
				<tr style="display: none;">
					<td width="20%">收费名称</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="CHARGE_NAME" id="input_CHARGE_NAME" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
					<td width="20%">单价</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="UNIT_PRICE" id="input_UNIT_PRICE" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
					<td width="20%">类型ID</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="CHARGETYPE_ID" id="input_CHARGETYPE_ID" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
				</tr>
				<tr style="display: none;">
					<td width="20%">商品ID</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="GOODS_ID" id="input_GOODS_ID" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
					<td width="20%">仓库编码</td>
					<td width="10%">
						<input type="text" style="width: 100%;" name="STOREHOUSE_ID" id="input_STOREHOUSE_ID" class="easyui-textbox" data-options="disabled:true,required:true" />
					</td>
				</tr>
			</table>
			<div id="dlg" class="easyui-dialog" closed="true" style="width:560px;height:400px;padding:10px">
				<iframe id="ifrmEdit" height="100%" width="100%" frameborder="0" scrolling="no"></iframe>
			</div>
			<div id="new_footer"></div>
		</form>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/FrozenShareFunction/FrozenShare.js"></script>
		<script type="text/javascript" src="../../../JS/MarketShareFunction/DialogShareFunction.js"></script>
	</body>
	<script type="text/javascript">
		var content;
		var id = getID();
		var currid;
		var isVerify;
		var verifyData;
		var InstorageId = -1;
		var detailData = getDetailData();
		var receipts_type = RECEIPT_UNVERIFY_SYMBOL + RECEIPT_PRINT_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL + RECEIPT_ADD_SYMBOL + RECEIPT_DELETE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		addDialogFooter($("#new_footer"));
		initData();
		initCombobox();

		$(document).ready(function() {
			if(id != AddFlag) {
				currid = id;
				var _url = RemoteLinkAddress + '/frozen/outorder/findbyid.do?ID=' + currid;
				getRemoteData(_url);
				$("#input_STOREHOUSE_NAME").combobox('options').disabled = true;
			} else {
				currid = AddFlag;
			}
		})

		//新建按钮
		function newdata() {
			$("#verifydisplay").empty(); //去掉审核或者未审核的图片
			isVerify = 0; //将状态改为未审核
			currid = AddFlag;
			resetData();
		}

		//保存按钮
		function save() {
			if(!isTrueData()) {
				$.messager.alert("错误", "请检查数据的完整性！");
				return;
			}
			var _url;
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/frozen/outorder/move.do"
			} else {
//				if(isVerify == 1) {
					$.messager.alert("警告", "移库单无法修改，请分别删除移库单和对应入库单!");
					return;
//				}
//				_url = RemoteLinkAddress + "/frozen/outorder/update.do";
			}
			saveData(_url);
		}

		//删除按钮
		function deletedata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "表单已审核，无法删除");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，无法删除");
				return;
			}
			$.messager.confirm("确认", "是否删除", function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/frozen/outorder/delete.do", {
						"ID": currid,
					}, function(result) {
						if(result.msg == "success") {
							$.messager.alert({
								title: '成功',
								msg: '删除成功!'
							});
							reflashDataGrid();
							var parentLocation;
							parentLocation = searchParent("LeasingOperationDetail.html");
							parentLocation.dialogClose();
						} else {
							$.messager.alert({
								title: 'Error',
								msg: result.msg
							});
						}
					}, 'json');
				}
			});
		}

		//审核按钮
		function verifydata() {
			if(isVerify == 1) {
				$.messager.alert("警告", "表单已审核，无须再审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不能执行");
				return;
			}
			$.messager.confirm("确认", "是否审核", function(r) {
				if(r) {
					var _data = reVerifyData(verifyData);
					$.ajax({
						type: "post",
						url: RemoteLinkAddress + "/frozen/outorder/verify.do",
						dataType: 'json',
						data: _data,
						success: function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '审核成功!'
								});
								verifyStatus_Market(1, true);
								isVerify = 1;
								reflashDataGrid();
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						}
					});
				}
			});
		}

		//弃审按钮
		function unverifydata() {
			if(isVerify == 0) {
				$.messager.alert("警告", "表单已弃审，无须再审");
				return;
			}
			if(currid == AddFlag) {
				$.messager.alert("警告", "该页面为新增页面，不能执行");
				return;
			}
			$.messager.confirm('Confirm', '是否弃审', function(r) {
				if(r) {
					var _data = reVerifyData(verifyData);
					$.ajax({
						type: "post",
						url: RemoteLinkAddress + "/frozen/outorder/unverify.do",
						dataType: 'json',
						data: _data,
						success: function(result) {
							if(result.msg == "success") {
								$.messager.alert({
									title: '成功',
									msg: '弃审成功!'
								});
								verifyStatus_Market(0, true);
								isVerify = 0;
								reflashDataGrid();
							} else {
								$.messager.alert({
									title: 'Error',
									msg: result.msg
								});
							}
						}
					});
				}
			});
		}

		//打印按钮
		function print() { //打印
			if(currid == AddFlag) {
				$.messager.alert('警示', "当前页面为新增页面，不可打印！");
				return;
			}
			if(currid == AddFlag || isVerify != 1) {
				$.messager.alert('警示', "该页面未审核，不可以打印！");
				return;
			}
			var pLocation = "LeasingOperationPrintOutAndRemoveStorage.html?id=" + currid + "&type=remove";
			window.open(pLocation);
		}

		//审核所需数据
		function reVerifyData(_data) {
			var data = {};
			data.AMOUNT = _data.AMOUNT;
			data.ID = _data.ID;
			data.ORDER_ID = _data.ORDER_ID;
			data.TYPE = _data.TYPE;
			return data;
		}

		//获取表头的数据
		function getDetailDatas(_data) {
			var data;
			var parentLocation;
			parentLocation = searchParent("LeasingOperationDetail.html");
			data = parentLocation.ReturnDetailData();
			_data.TYPE = "1";
			_data.TENANT_NAME = data.TENANT_NAME;
			_data.TENANT_ID = data.TENANT_ID;
			_data.PARTNER1 = data.PARTNER1;
			_data.PARTNER1_ID = data.PARTNER1_ID;
			_data.PARTNER2 = data.PARTNER2;
			_data.PARTNER2_ID = data.PARTNER2_ID;
			_data.PARTNER3 = data.PARTNER3;
			_data.PARTNER3_ID = data.PARTNER3_ID;
			_data.ID = data.ID;
			_data.FROZEN_FEE = "0";
			return _data;
		}

		//提交数据(保存为入库单)
		function saveData(_url) {
			lockButton("lock");
			var _data = submitDialogData();
			_data = getDetailDatas(_data);
			if(currid != AddFlag) {
				_data.ID = currid;
			}
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: _data,
				type: "post",
				success: function(result) {
					lockButton("unlock");
					if(result.msg == "zero") {
						$.messager.alert("消息", "库存不足");
					} else {
						if(result.msg.search("success") > -1) {
							$.messager.alert("消息", "保存成功");
							reflashDataGrid();
							
							var parentLocation;
							parentLocation = searchParent("LeasingOperationDetail.html");
							parentLocation.dialogClose();
							//						saveDataIn(_data);
						} else {
							$.messager.alert("提示", "保存失败");
						}
					}

				},
				error: function(result) {
					$.messager.alert("错误", "保存失败");
				}
			});
		}

		//提交数据升成新的入库单
		function saveDataIn(_data) {
			var _url;
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/frozen/inorder/save.do"
			} else {
				_url = RemoteLinkAddress + "/frozen/inorder/update.do";
				_data.ID = InstorageId;
				console.log(_data.ID);
				delete _data.ORDER_ID;
			}
			$.ajax({
				dataType: 'JSON',
				url: _url,
				data: _data,
				type: "post",
				success: function(result) {
					if(result.msg.search("success") > -1) {} else {
						$.messager.alert("提示", "保存入库单失败");
					}
				},
				error: function(result) {
					$.messager.alert("错误", "保存入库单失败");
				}
			});
		}

		//获取对应的入库单的ID
		function getInstorageId() {
			var data = getDetailData();
			var tenant_id = data.TENANT_ID;
			var goods_id = data.GOODS_ID;
			var storehouse_id = verifyData.STOREHOUSE_ID;
			var _url = RemoteLinkAddress + "/frozen/inorder/list.do?TENANT_ID=" + tenant_id + "&GOODS_ID=" + goods_id + "&STOREHOUSE_ID=" + storehouse_id;
			$.ajax({
				type: "get",
				url: _url,
				success: function(result) {
					var data = result.rows;
					if(data[0].ID != null || data[0].ID) {
						InstorageId = data[0].ID;
					}
				}
			});
		}

		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						isVerify = result.ISVERIFY;
						currid = result.ID;
						verifyData = result;
						getDialogData(result);
						verifyStatus_Market(isVerify, true);
						getInstorageId();
					}
				}
			});
		}

		//判断数据的完整性
		function isTrueData() {
			if($("#input_STOREHOUSE_NAME").combobox('getValue').trim().length == 0) {
				return false;
			}
			if($("#input_AMOUNT").val().trim().length == 0) {
				return false;
			}
			return true;
		}

		//刷新datagrid
		function reflashDataGrid() {
			var data = detailData;
			var id = data.ID;
			var _url = RemoteLinkAddress + "/frozen/inorder/findbyid.do?ID=" + 　id;
			var parentLocation;
			parentLocation = searchParent("LeasingOperationDetail.html");
			parentLocation.getRemoteData(_url);
			return data;
		}

		function getDetailData() {
			var parentLocation;
			parentLocation = searchParent("LeasingOperationDetail.html");
			var data = parentLocation.ReturnDetailData();
			console.log(data);
			return data;
		}

		//初始化
		function initData() {
			var data = deepCopy(detailData);
			delete data.rows;
			delete data.rows2;
			delete data.AMOUNT;
			delete data.STOREHOUSE_NAME;
			delete data.HAND_FEE;
			delete data.OTHRE_FEE;
			delete data.SUM;
			delete data.REAL_SUM;
			var _name;
			$("#table input[name]").each(function(index) {
				_name = $(this).attr("name");
				if(data[_name] != null || data[_name] != undefined) {
					$(this).val(data[_name]);
				} else {
					$(this).val("");
				}
			});
		}

		//初始化combobox
		function initCombobox() {
			createShoreHouseCombobox();

		}
	</script>

</html>